import {AfterViewInit, Component} from "@angular/core";
import {BaseComponent} from "../../base.component";
import {ArrayModel} from "../../model/ArrayModel";
import {ms_article} from "../../model/ms_article";
import {ArticleService} from "../article.service";
import {ArticleTypeEnum} from "../../model/ArticleTypeEnum";


declare const UM:any;

@Component({
    selector: 'my-rightContent',
    templateUrl:"./article.write.component.html",
    styleUrls:["./article.write.component.css"]

})
export class ArticleWrite extends BaseComponent implements AfterViewInit{


    _datasource:ArrayModel<ms_article>=new ArrayModel(ms_article);

    _dataFromID:ms_article=new ms_article();

    _data:ms_article=null;

    _type:ArticleTypeEnum=ArticleTypeEnum.article;

    _UM:any=null;

    constructor(private service:ArticleService){
        super();
        this._data=service.data;
        this._data.setType(this._type);
    }


    ngAfterViewInit(): void {
        UM.clearCache("myEditor");
        this._UM = UM.getEditor('myEditor');

    }


    handleSubmit(){

        this.isAjax=true;

        let parent=this;
        this._data.Content=this._UM.getContent();
        this.service.createArticle(this._data)
            .then((response)=>{
                parent.isAjax=false;
                if(response.result){
                    parent.dialog.successShow("文章发布成功");

                    parent.service.data=new ms_article();
                    this._UM.setContent("");
                    parent._data=parent.service.data;
                    parent._data.setType(this._type);
                    //document.getElementById("myEditor").innerHTML="";
                }else {
                    parent.dialog.dangerShow(response.message);
                }

            }).catch((ex)=>parent.setMessage(ex))
    }

}
